package org.jeecg.modules.feeElectricity.scheduled;

import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.chargeElectricity.entity.ChargeElectricityDetail;
import org.jeecg.modules.chargeElectricity.service.IChargeElectricityService;
import org.jeecg.modules.consumptionElectricity.service.IConsumptionElectricityService;
import org.jeecg.modules.feeElectricity.entity.FeeElectricity;
import org.jeecg.modules.feeElectricity.service.IFeeElectricityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Calendar;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@Configuration
@EnableScheduling
@Slf4j
public class FeeElectricityScheduled {

    private final IFeeElectricityService feeElectricityService;
    private final IConsumptionElectricityService consumptionElectricityService;
    private final IChargeElectricityService chargeElectricityService;

    @Autowired
    public FeeElectricityScheduled(IFeeElectricityService feeElectricityService, IConsumptionElectricityService consumptionElectricityService, IChargeElectricityService chargeElectricityService) {
        this.feeElectricityService = feeElectricityService;
        this.consumptionElectricityService = consumptionElectricityService;
        this.chargeElectricityService = chargeElectricityService;
    }

    @Scheduled(cron = "0 59 * * * *")//每小时的59分执行一次
    public void configureTasks() throws Exception{
        log.info("执行静态定时任务时间: {}" , LocalDateTime.now());
        DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHH");
        Calendar calendar = Calendar.getInstance();
        String today = dateFormat.format(calendar.getTime());

        int year = calendar.get(Calendar.YEAR);
        int month = (calendar.get(Calendar.MONTH) + 1);
        int day = calendar.get(Calendar.DATE);
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        Integer date = Integer.valueOf(today);

        List<ChargeElectricityDetail> programme = chargeElectricityService.getProgramme();
        BigDecimal price = BigDecimal.ZERO;
        for (ChargeElectricityDetail chargeElectricityDetail : programme){
            int startTime = chargeElectricityDetail.getStartTime();
            int endTime = chargeElectricityDetail.getEndTime();
            if (startTime <= hour && hour <= endTime){
                price = chargeElectricityDetail.getPrice();
            }
        }
        List<Map<String, Object>> todayConsumptions = consumptionElectricityService.getDayConsumption(year, month, day,hour);
        List<FeeElectricity> list = new LinkedList<>();
        for (Map<String, Object> todayConsumption : todayConsumptions){
            String dept = (String) todayConsumption.get("dept");
            BigDecimal consumption = new BigDecimal(String.valueOf(todayConsumption.get("consumption")));
            BigDecimal result = price.multiply(consumption).setScale(2, RoundingMode.HALF_UP);
            FeeElectricity feeElectricity = new FeeElectricity();
            feeElectricity.setFee(result).setDept(dept).setYear(String.valueOf(year))
                    .setMonth(String.valueOf(month)).setDay(String.valueOf(day)).setHour(String.valueOf(hour)).setDate(date);
            list.add(feeElectricity);
        }
        if(list.isEmpty()){
            log.info("即将插入的【电费】数据: {},无数据不插入" , list);
            return;
        }
        log.info("即将插入的【电费】数据: {}" , list);
        feeElectricityService.saveBatch(list);
    }
    
}
